﻿using Microsoft.AspNetCore.Mvc;
using ZR.Admin.WebApi.Filters;
using ZR.Model.Dto.Business.ProductionControl;
using ZR.Model.Models.ProductionControl;
using ZR.Service.IService.ProductionControl;
using ZR.Service.Service.ProductionControl;

//创建时间：2025-05-12
namespace ZR.Admin.WebApi.Controllers
{
    /// <summary>
    /// 工艺主表
    /// </summary>
    [Verify]
    [Route("business/CksTechnique")]
    [ApiExplorerSettings(GroupName = "business")]
    public class CksTechniqueController : BaseController
    {
        /// <summary>
        /// 工艺主表接口
        /// </summary>
        private readonly ICksTechniqueService _CksTechniqueService;

        public CksTechniqueController(ICksTechniqueService CksTechniqueService)
        {
            _CksTechniqueService = CksTechniqueService;
        }

        /// <summary>
        /// 查询工艺主表列表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        [HttpGet("list")]
        [ActionPermissionFilter(Permission = "ckstechnique:list")]
        public IActionResult QueryCksTechnique([FromQuery] CksTechniqueQueryDto parm)
        {
            var response = _CksTechniqueService.GetList(parm);
            return SUCCESS(response);
        }


        /// <summary>
        /// 查询工艺主表详情
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpGet("{Id}")]
        [ActionPermissionFilter(Permission = "ckstechnique:query")]
        public IActionResult GetCksTechnique(int Id)
        {
            var response = _CksTechniqueService.GetInfo(Id);

            var info = response.Adapt<CksTechniqueDto>();
            return SUCCESS(info);
        }

        /// <summary>
        /// 添加工艺主表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [ActionPermissionFilter(Permission = "ckstechnique:add")]
        [Log(Title = "工艺主表", BusinessType = BusinessType.INSERT)]
        public IActionResult AddCksTechnique([FromBody] CksTechniqueDto parm)
        {
            var modal = parm.Adapt<CksTechnique>().ToCreate(HttpContext);
            modal.Nullity = true;
            var response = _CksTechniqueService.AddCksTechnique(modal);

            return SUCCESS(response);
        }

        /// <summary>
        /// 更新工艺主表
        /// </summary>
        /// <returns></returns>
        [HttpPut]
        [ActionPermissionFilter(Permission = "ckstechnique:edit")]
        [Log(Title = "工艺主表", BusinessType = BusinessType.UPDATE)]
        public IActionResult UpdateCksTechnique([FromBody] CksTechniqueDto parm)
        {
            var modal = parm.Adapt<CksTechnique>().ToUpdate(HttpContext);
            var response = _CksTechniqueService.UpdateCksTechnique(modal);

            return ToResponse(response);
        }

        /// <summary>
        /// 删除工艺主表
        /// </summary>
        /// <returns></returns>
        [HttpDelete("delete/{ids}")]
        [ActionPermissionFilter(Permission = "ckstechnique:delete")]
        [Log(Title = "工艺主表", BusinessType = BusinessType.DELETE)]
        public IActionResult DeleteCksTechnique([FromRoute] string ids)
        {
            var client = _CksTechniqueService.AsSugarClient();
            var idArr = Tools.SplitAndConvert<int>(ids);

            client.Deleteable<CksTechniqueDetails>().Where(a => idArr.Contains(a.TechniqueId)).ExecuteCommand();
            return ToResponse(_CksTechniqueService.Delete(idArr));
        }

        //查询工艺模板
        [HttpPost("GetTemplates")]
        public IActionResult GetTemplates()
        {
            var client = _CksTechniqueService.AsSugarClient();
            var response = client.Queryable<CksTechniqueTemplate>().Where(a=>a.Type == 1).OrderBy(a => a.Sequence).ToList();
            return SUCCESS(response);
        }


    }
}